using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ConnectionEngine.Common;
using Microsoft.ConnectionEngine.Adapters;

namespace Microsoft.ConnectionEngine.Samples.Adapters
{
    public class SystemAdapterSample : SystemAdapterBase
    {
        /// <summary>
        /// This implementation of AssembleMessage receives a string representing as the OuterXML of a
        /// valid payload and returns a XMLDocument constructed out of the OuterXML. Optionally, this can receive an XML Document
        /// representing the valid payload and return that XML Document.
        /// </summary>
        /// <param name="objMessage">
        /// An object representing a valid payload. It should be a string representing an OuterXML
        /// or an XML Document.
        /// </param>
        /// <param name="msgType">The unique identifier of the message type that this payload represents.</param>
        /// <returns>Returns an XML Document representing the assembled payload.</returns>
        protected override System.Xml.XmlDocument AssembleMessage(Object objMessage, int msgType)
        {
            try
            {
                if (objMessage.GetType() == typeof(string))
                {
                    System.Xml.XmlDocument result = new System.Xml.XmlDocument();
                    result.LoadXml((string)objMessage);
                    return result;
                }
                else
                {
                    return (System.Xml.XmlDocument)objMessage;
                }
            }
            catch (Exception ex)
            {
                throw new SystemAdapterException(
                    SystemAdapterExceptionCause.Hl7PlayloadCreation,
                    "Error Assembling Message Payload",
                    ex);
            }
        }

        /// <summary>
        /// This sample implementation of the ProcessMessage saves the payload received as a parameter and
        /// saves it to the file system.
        /// </summary>
        /// <param name="xmlMessage">The payload to be processed.</param>
        /// <param name="msgType">The message type representing the payload.</param>
        /// <param name="resultXmlMessage">The resulting XML. In this sample implementation, the returning payload will always be the same as the payload received as a parameter.</param>
        /// <param name="resultMsgType">The resulting message type. In this sample implementation, the returning message type will always be zero.</param>
        protected override void ProcessMessage(System.Xml.XmlDocument xmlMessage, int msgType, out System.Xml.XmlDocument resultXmlMessage, out int resultMsgType)
        {
            // Defines File pathname           
            string timestamp = DateTime.Now.Year.ToString() + "_"
            + DateTime.Now.Month.ToString() + "_"
            + DateTime.Now.Day.ToString() + "_"
            + DateTime.Now.Hour.ToString() + "_"
            + DateTime.Now.Minute.ToString() + "_"
            + DateTime.Now.Second.ToString();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "");
            path = System.Configuration.ConfigurationManager.AppSettings["SystemAdapterExample.ProcessMessage.IncomingFolder"].Replace(".", path);
            string location = path + @"\"
                                   + System.Configuration.ConfigurationManager.AppSettings["SystemAdapterExample.ProcessMessage.MessagePrefix"] + "_"
                                   + msgType.ToString() + "_"
                                   + timestamp + ".xml";

            //Writes XmlMessage to defined pathname.
            System.IO.TextWriter tw = new System.IO.StreamWriter(location, false);
            tw.Write(xmlMessage.OuterXml);
            tw.Close();

            // Defines returning xml message.
            resultXmlMessage = xmlMessage;
            resultMsgType = 0;
        }
    }
}
